import Vue from 'vue';
import Axios from 'axios';
import Qs from 'qs';
import VModal from 'vue-js-modal';
import '../basic/basic.css';
Vue.use(VModal,{dialog:true});
new Vue({
    el:'#louie-setting',
    data:{
        //--公共--
        url:$('#louie-setting').attr('data-url'),
        //--nav--
        nav:[
            {name:'公众号设置',mark:'wx',active:true},
            {name:'地图设置',mark:'map',active:false},
            {name:'财务设置',mark:'finance',active:false},
            {name:'其它设置',mark:'other',active:false}
        ],
        navIndex:0,
        //--公众号--
        appid:'',
        app_secret:'',
        mcht_account:'',
        mcht_key:'',
        //--高德地图--
        map_key:'',
        //--财务设置--
        deposit_check:0,
        deposit_enter_detail:'',
        //--其它--
        domain_defend:0,  //是否开启域名防封
        deposit_limit:1.00  //用户最低提现金额限制
    },
    created:function(){
        this.find();
    },
    methods:{
        //切换导航
        changeNav:function(index){
            if(index != this.navIndex){
                this.nav[this.navIndex].active = false;
                this.navIndex = index;
                this.nav[index].active = true;
            }
        },
        //提交
        submit:function(){
            switch(this.nav[this.navIndex].mark){
                case 'wx':
                    this.setWx();
                    break;
                case 'map':
                    this.setMap();
                    break;
                case 'finance':
                    this.setFinance();
                    break;
                case 'other':
                    this.setOther();
                    break;
            }
        },
        //设置公众号参数
        setWx:function(){
            var data = {
                appid:this.appid,
                app_secret:this.app_secret,
                mcht_account:this.mcht_account,
                mcht_key:this.mcht_key
            };
            Axios.post(this.url,Qs.stringify({
                pages:'setting',
                doing:'set',
                data:data
            }))
                .then(res=>{
                    var data = res.data;
                    if(data.status == 'success'){
                        this.alert('修改成功');
                    }else{
                        this.alert('修改失败');
                    }
                })
                .catch(error=>console.log(error));
        },
        //设置地图
        setMap:function(){
            var data = {map_key:this.map_key};
            Axios.post(this.url,Qs.stringify({
                pages:'setting',
                doing:'set',
                data:data
            }))
                .then(res=>{
                    var data = res.data;
                    if(data.status == 'success'){
                        this.alert('修改成功');
                    }else{
                        this.alert('修改失败');
                    }
                })
                .catch(error=>console.log(error));
        },
        //财务提现设置
        setFinance:function(){
            if(!/(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/.test(this.deposit_limit)){
                this.alert('请输入正确的最低提现金额');
                return false;
            }
            if(parseFloat(this.deposit_limit) < 1){
                this.alert('最低提现金额不能小于1元');
                return false;
            }
            if(!/(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/.test(this.deposit_check)){
                this.alert('请输入正确审核限制金额');
                return false;
            }
            if(this.deposit_enter_detail != null && this.deposit_enter_detail.length > 20){
                this.alert('入账详情自定义内容长度不能大于20');
                return false;
            }
            var data = {
                deposit_limit:this.deposit_limit,
                deposit_check:this.deposit_check,
                deposit_enter_detail:this.deposit_enter_detail
            }
            Axios.post(this.url,Qs.stringify({
                pages:'setting',
                doing:'set',
                data:data
            }))
                .then(res=>{
                    var data = res.data;
                    if(data.status == 'success'){
                        this.alert('修改成功');
                    }else{
                        this.alert('修改失败');
                    }
                })
                .catch(error=>console.log(error));
        },
        //其它设置
        setOther:function(){
            var data = {
                domain_defend:this.domain_defend
            };
            Axios.post(this.url,Qs.stringify({
                pages:'setting',
                doing:'set',
                data:data
            }))
                .then(res=>{
                    var data = res.data;
                    if(data.status == 'success'){
                        this.alert('修改成功');
                    }else{
                        this.alert('修改失败');
                    }
                })
                .catch(error=>console.log(error));
        },
        //切换域名防封触发
        defendChange:function(){
            if(this.domain_defend == 1){
                Axios.post(this.url,Qs.stringify({
                    pages:'setting',
                    doing:'checkDomainDefend'
                }))
                    .then((res)=>{
                        if(res.data.status == 'error'){
                            this.domain_defend = 0;
                            this.alert('开启失败，你可能没有安装模块:酷源防封');
                        }
                    })
                    .catch(error=>console.log(error));
            }
        },
        //获取数据
        find:function(){
            Axios.post(this.url,Qs.stringify({
                pages:'setting'
            }))
                .then(res=>{
                    var data = res.data;
                    if(data.status == 'success'){
                        this.appid = data.data.appid;
                        this.app_secret = data.data.app_secret;
                        this.mcht_account = data.data.mcht_account;
                        this.mcht_key = data.data.mcht_key;
                        this.map_key = data.data.map_key;
                        this.domain_defend = data.data.domain_defend;
                        this.deposit_limit = data.data.deposit_limit;
                        this.deposit_check = data.data.deposit_check;
                        this.deposit_enter_detail = data.data.deposit_enter_detail;
                    }
                })
                .catch(error=>console.log(error));
        },
        //上传证书
        uploadPem:function(ev){
            var data = new FormData();
            data.append('pages','setting');
            data.append('doing','uploadPem');
            for(var i=0;i<ev.target.files.length;i++){
                data.append('myFile[]',ev.target.files[i]);
            }
            Axios.post(this.url,data,{
                headers:{'X-Requested-With':'XMLHttpRequest'}
            })
                .then(res=>{
                    var data = res.data;
                    if(data.status == 'error'){
                        this.alert(data.err_msg);
                    }else{
                        this.alert('上传证书成功');
                    }
                })
                .catch(error=>console.log(error));
        },
        //alert
        alert:function(text){
            this.$modal.show('dialog',{
                text:text,
                buttons:[
                    {title:'关闭'}
                ]
            });
        }
    }
});